package com.atguigu.videoservice.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.videoservice.service.VideoService;
import com.atguigu.videoservice.util.ConstantPropertiesUtil;
import com.atguigu.videoservice.util.InitVodObject;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description="视频管理")
@CrossOrigin
@RestController
@RequestMapping("eduvod/")
public class VideoController {

    @Autowired
    VideoService videoService;

    @PostMapping("upload")
    public R uploadVideo(MultipartFile file){
        String videoId = videoService.uploadVideo(file);
        return R.ok().data("videoId",videoId);
    }

    @DeleteMapping("deleteVideoFile")
    public R deleteVideoFile(@RequestParam("sourceIdList") List<String> sourceIdList){
        boolean status = videoService.deleteVideoFile(sourceIdList);
        return status?R.ok():R.error();
    }

    @GetMapping("getplayauth/{videoSourceId}")
    public R getPlayAuth(@PathVariable("videoSourceId") String videoSourceId) throws ClientException {
        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = InitVodObject.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoSourceId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }



}